import json
import logging as log

from com.maycur.gateway.common.base_entity import SequentialApiTaskSet
from com.maycur.utils import TimeUtils
from locust import task


class DepartmentTask(SequentialApiTaskSet):
    root_department_code: str
    root_department_name: str
    new_department_code: str
    new_department_name_suffix: str
    child_count: int

    @task
    def get_root_department(self):
        root_department_url = f'{self.get_app_host()}/api/web/config/org/department/root/tree'

        with self.client.get(root_department_url, headers=self.get_headers(), catch_response=True) as res:
            result_dto = self.parse_response(res)
            if result_dto is None:
                return

            root_department = result_dto.data[0]
            self.root_department_code = root_department['code']
            self.root_department_name = root_department['name']

    @task
    def get_root_department_detail(self):
        department_detail_url = f'{self.get_app_host()}/api/web/config/org/department/{self.root_department_code}'
        with self.client.get(department_detail_url, headers=self.get_headers(), catch_response=True) as res:
            self.parse_response(res)

    @task
    def get_children_department(self):
        child_department_url = f'{self.get_app_host()}/api/web/config/org/department/{self.root_department_code}/tree'
        with self.client.get(child_department_url, headers=self.get_headers(), catch_response=True) as res:
            result_dto = self.parse_response(res)
            if result_dto is None:
                return

            self.child_count = len(result_dto.data)
            log.info(f'{self.root_department_name}有{self.child_count}个子部门')

    def get_department_update_request_body(self, name_suffix: str):
        return {
            "name": f'新增子部门 - {name_suffix}',
            "nameZh": f'New sub-department - {name_suffix}',
            "parentCode": [{
                "code": self.root_department_code,
                "name": self.root_department_name
            }],
            "parentDepartment": {
                "code": self.root_department_code
            },
            "enabled": True
        }

    @task
    def save_new_department(self):
        save_dept_url = f'{self.get_app_host()}/api/web/config/org/department'
        name_suffix = TimeUtils.current_date_time_with_random()
        self.new_department_name_suffix = name_suffix
        request_body = self.get_department_update_request_body(name_suffix)
        json_body = json.dumps(request_body)
        headers = self.get_headers_with_content_type()
        with self.client.put(save_dept_url, headers=headers, data=json_body, catch_response=True) as res:
            result_dto = self.parse_response(res)
            if result_dto is None:
                return

            self.new_department_code = result_dto.data['code']

    @task
    def disable_department(self):
        """
        禁用刚新增的部门
        """
        if not hasattr(self, 'new_department_code'):
            return

        update_dept_url = f'{self.get_app_host()}/api/web/config/org/department'
        request_body = self.get_department_update_request_body(self.new_department_name_suffix)
        request_body['code'] = self.new_department_code
        request_body['enable'] = False
        json_body = json.dumps(request_body)
        headers = self.get_headers_with_content_type()
        with self.client.put(update_dept_url, headers=headers, data=json_body, catch_response=True) as res:
            self.parse_response(res)

    @task(1)
    def remove_department(self):
        """
        删除刚新增的部门
        """
        if not hasattr(self, 'new_department_code'):
            return

        delete_dept_url = f'{self.get_app_host()}/api/web/config/org/department/{self.new_department_code}'
        with self.client.delete(delete_dept_url, headers=self.get_headers(), catch_response=True) as res:
            self.parse_response(res)
